package com.cn.xcsharp.controller;

import com.cn.xcsharp.bean.UserBean;
import com.cn.xcsharp.service.IPublicDataService;
import com.cn.xcsharp.util.CommonResult;
import com.cn.xcsharp.util.JwtUtil;
import com.cn.xcsharp.util.MD5Utils;
import com.cn.xcsharp.util.StrUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主接口   -  oracle
 */
@RestController
@RequestMapping("/api/v1")
public class SysController {
    private static final Logger log = LoggerFactory.getLogger(SysController.class);

    @Resource
    private IPublicDataService Ipds;
    //登录
    @PostMapping(value = "/login")
    public CommonResult login(@RequestBody UserBean sysUser){
        Map map = new HashMap<>();
        String username = sysUser.getUsername();
        String password = sysUser.getPassword();

        CommonResult result = new CommonResult();
        password=new MD5Utils().getMD5ofStr(username.toUpperCase()+password);
        String sql="select * from S_USERS t where name='"+username+"' and password='"+password+"'";
        List list=Ipds.getList(sql,new  Object[]{});
        if(list.size()>0){
            String token = JwtUtil.sign(username,password);
            if (token != null){
                map.put("token", token);
                result.setMsg(StrUtils.INFO_200);
                result.setState(200);
                result.setData(map);
            }else{
                result.setMsg(StrUtils.INFO_602);
                result.setState(602);
            }
        }else{
            result.setMsg(StrUtils.INFO_602);
            result.setState(602);
        }
        return result;
    }
}

